package com.api.shopapi.service.impl;

import com.api.shopapi.entity.Mediciner;
import com.api.shopapi.entity.MedicinerStaffRel;
import com.api.shopapi.entity.User;
import com.api.shopapi.entity.dto.MedicinerDTO;
import com.api.shopapi.mapper.MedicinerMapper;
import com.api.shopapi.service.MedicinerService;
import com.api.shopapi.service.MedicinerStaffRelService;
import com.api.shopapi.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("MedicinerServiceImplOne")
public class MedicinerServiceImpl implements MedicinerService {

    @Autowired
    private MedicinerMapper medicinerMapper;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;

    @Autowired
    @Qualifier("MedicinerStaffRelServiceImplOne")
    private MedicinerStaffRelService medicinerStaffRelService;

    @Override
    public List<MedicinerDTO> getList(String institutionCode) {
        return medicinerMapper.getList(institutionCode);
    }

    @Override
    public int insert(Mediciner mediciner) {
        return medicinerMapper.insert(mediciner);
    }

    @Override
    @Transactional
    public int updateByMedicinerCode(User user,Mediciner mediciner) {
        userService.update(user);
        return medicinerMapper.updateByMedicinerCode(mediciner);
    }

    @Override
    public int deleteByMedicinerCode(String medicinerCode) {
        return medicinerMapper.deleteByMedicinerCode(medicinerCode);
    }

    @Override
    public Mediciner getByMedicinerCode(String medicinerCode) {
        return medicinerMapper.getByMedicinerCode(medicinerCode);
    }

    @Override
    @Transactional
    public int regInsert(User user, Mediciner mediciner) {
        userService.insert(user);
        return medicinerMapper.insert(mediciner);
    }

    @Override
    public Mediciner getByUserCode(String userCode) {
        return medicinerMapper.getByUserCode(userCode);
    }

    @Override
    public
    Mediciner getByIdCard(String idCard){
        return medicinerMapper.getByIdCard(idCard);
    }


    @Override
    public PageInfo<MedicinerDTO> getPageList(int pageIndex, int pageSize, String institutionCode, String name, int status) {
        PageHelper.startPage(pageIndex, pageSize);
        List<MedicinerDTO> medicinerDTOList = medicinerMapper.getPageList(institutionCode,name,status);
        PageInfo<MedicinerDTO> pageInfo = new PageInfo<>(medicinerDTOList);
        return pageInfo;
    }

    @Override
    public
    PageInfo<MedicinerDTO> pageByGovCode(int pageIndex, int pageSize, String governmentCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<MedicinerDTO> medicinerDTOList = medicinerMapper.listByGovCode(governmentCode);
        PageInfo<MedicinerDTO> pageInfo = new PageInfo<>(medicinerDTOList);
        return pageInfo;
    }

    @Override
    public
    PageInfo<Mediciner> pageByGovCodeInsCode(int pageIndex, int pageSize, String governmentCode,
                                             String institutionCode){
        PageHelper.startPage(pageIndex, pageSize);
        List<Mediciner> medicinerDTOList = medicinerMapper.listByGovCodeInsCode(governmentCode,institutionCode);
        PageInfo<Mediciner> pageInfo = new PageInfo<>(medicinerDTOList);
        return pageInfo;
    }

    @Override
    public
    List<MedicinerDTO> listByGovCode(String governmentCode){
        return medicinerMapper.listByGovCode(governmentCode);
    }

    @Override
    public
    List<Mediciner> listByInsCode(String institutionCode){
        return medicinerMapper.listByInsCode(institutionCode);
    }

    @Override
    public String getStaffCode(String userCode) {
        return medicinerMapper.getStaffCodeByUserCode(userCode);
    }

    @Override
    public void updateIdCardAndInstitution(Mediciner mediciner) {
         medicinerMapper.updateIdCardAndInstitution(mediciner);
    }


}
